﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace IPSWWA.Models

	/*NOTE
	At the time I made these I was new(ish) to linqtosql, so I'm not sure if 
	there's a better way. The objects all work like they're intended. So haven't
	been too willing to change... yet */




    public interface IWorkingLists
    {
        List<WorkingObject> Working { get; set; }
        IQueryable AllWorking { get; set; }
    }

    public class WorkingObject
    {
        public Record Record { get; set; }
        public Keydr Doctor { get; set; }
        public Keydrug Drug { get; set; }
        public Keydrugcompany DrugCompany { get; set; }
        public Keydrugrequirement DrugRequirement { get; set; }
        public Keyhospital Hospital { get; set; }
        public Keypatient Patient { get; set; }
        public Keyunit Unit { get; set; }
        public Keydrmatch DoctorMatch { get; set; }
        public Keypatientmatch PatientMatch { get; set; }

        public WorkingObject(int id)
        {
            KeyRepository Repo = new KeyRepository();
            Record = Repo.RecordbyID(id).FirstOrDefault();
            Doctor = Repo.DoctorbyID(Record.DoctorID).FirstOrDefault();
            Drug = Repo.DrugbyID(Record.DrugID).FirstOrDefault();
            DrugCompany = Repo.DrugCompany(Drug.CompanyID).FirstOrDefault();
            DrugRequirement = Repo.DrugRequirementbyID(Drug.ID).FirstOrDefault();
            Hospital = Repo.HospitalbyID(Record.FacID).FirstOrDefault();
            Patient = Repo.PatientbyID(Record.PatientID).FirstOrDefault();
        }
    }

    public class AllWorkingObject : IWorkingLists
    {
        public List<WorkingObject> Working { get; set; }
        public IQueryable AllWorking { get; set; }


        public AllWorkingObject()
        {
            KeyRepository Repo = new KeyRepository();
            Working = new List<WorkingObject>();
            AllWorking = Repo.AllWorking();
            foreach (Record AllofWorking in AllWorking)
            {
                WorkingObject AddRecord = new WorkingObject(AllofWorking.ID);
                Working.Add(AddRecord);
            }
        }
    }

    public class AllPatientRecordsObject : IWorkingLists
    {
        public List<WorkingObject> Working { get; set; }
        public IQueryable AllWorking { get; set; }

        public AllPatientRecordsObject(int id)
        {
            KeyRepository Repo = new KeyRepository();
            Working = new List<WorkingObject>();
            AllWorking = Repo.Allpatientrecords(id);
            foreach (Record AllofWorking in AllWorking)
            {
                WorkingObject AddRecord = new WorkingObject(AllofWorking.ID);
                Working.Add(AddRecord);
            }
        }
    }

    public class DeltaWorkingObject : IWorkingLists
    {
        public List<WorkingObject> Working { get; set; }
        public IQueryable AllWorking { get; set; }

        public DeltaWorkingObject()
        {
            KeyRepository Repo = new KeyRepository();
            Working = new List<WorkingObject>();
            AllWorking = Repo.DeltaWorking();
            foreach (Record AllofWorking in AllWorking)
            {
                WorkingObject AddRecord = new WorkingObject(AllofWorking.ID);
                Working.Add(AddRecord);
            }
        }
    }


    public class WaitingObject
    {
        public Record Record { get; set; }
        public Keydr Doctor { get; set; }
        public Keydrug Drug { get; set; }
        public Keydrugcompany DrugCompany { get; set; }
        public Keyhospital Hospital { get; set; }
        public Keypatient Patient { get; set; }
        public Keypricing Pricing { get; set; }

        public WaitingObject(int id)
        {
            KeyRepository Repo = new KeyRepository();
            Record = Repo.RecordbyID(id).FirstOrDefault();
            Doctor = Repo.DoctorbyID(Record.DoctorID).FirstOrDefault();
            Drug = Repo.DrugbyID(Record.DrugID).FirstOrDefault();
            DrugCompany = Repo.DrugCompany(Drug.CompanyID).FirstOrDefault();
            Hospital = Repo.HospitalbyID(Record.FacID).FirstOrDefault();
            Patient = Repo.PatientbyID(Record.PatientID).FirstOrDefault();
            Pricing = Repo.DrugPricingbyID(Drug.ID).FirstOrDefault();

        }

    }


    public class AllWaitingObject
    {
        public List<WaitingObject> Waiting { get; set; }
        public IQueryable AllWaiting { get; set; }


        public AllWaitingObject()
        {
            KeyRepository Repo = new KeyRepository();
            Waiting = new List<WaitingObject>();
            AllWaiting = Repo.AllWaiting();
            foreach (Record AllofWaiting in AllWaiting)
            {
                WaitingObject AddRecord = new WaitingObject(AllofWaiting.ID);
                Waiting.Add(AddRecord);
            }
        }
    }

    public class FullDrugObject
    {
        public Keydrug Drug { get; set; }
        public Keydrugcompany Company { get; set; }
        public Keydrugrequirement Requirements { get; set; }

        public FullDrugObject(int id)
        {
            KeyRepository Repo = new KeyRepository();
            Drug = Repo.DrugbyID(id).FirstOrDefault();
            Company = Repo.DrugCompany(Drug.CompanyID).FirstOrDefault();
            Requirements = Repo.DrugRequirementbyID(Drug.ID).FirstOrDefault();
        }
    }

    public class AllDrugObject
    {
        public List<FullDrugObject> Drugs { get; set; }
        public IQueryable AllDrugs { get; set; }


        public AllDrugObject()
        {
            KeyRepository Repo = new KeyRepository();
            Drugs = new List<FullDrugObject>();
            AllDrugs = Repo.AllDrugs();
            foreach ( Keydrug Thesedrugs in AllDrugs)
            {
                FullDrugObject AddDrug = new FullDrugObject(Thesedrugs.ID);
                Drugs.Add(AddDrug);
            }
        }
    }

    public class AllCompanyObject
    {
        public List<Keydrugcompany> Companies { get; set; }
        public IQueryable AllCompanies { get; set; }

        public AllCompanyObject()
        {
            KeyRepository Repo = new KeyRepository();
            Companies = new List<Keydrugcompany>();
            AllCompanies = Repo.AllDrugCompany();
            foreach (Keydrugcompany Thesecompanies in AllCompanies)
            {
                Companies.Add(Thesecompanies);
            }
        }
    }


    /*Matching objects are below here.
     * Need to figure out a good interface for them I guess
     */

    public interface IMatchObject
    {
        Record Thisrecord { get; set; }
        Keyhospital Hospital { get; set; }
    }



    
    public class MatchDoctorObject : IMatchObject
    {
        public Record Thisrecord { get; set; }
        public Keyhospital Hospital { get; set; }

        public IQueryable<Keydr> Docs { get; set; }

        public MatchDoctorObject(int id)
        {
            KeyRepository Repo = new KeyRepository();
            Thisrecord = Repo.RecordbyID(id).Single();
            Docs = Repo.Doctormatch(Thisrecord);
            Hospital = Repo.HospitalbyID(Thisrecord.FacID).FirstOrDefault();
        }
    }

    public class MatchPatientObject : IMatchObject
    {
        public Record Thisrecord { get; set; }
        public Keyhospital Hospital { get; set; }

        public IQueryable<Keypatient> Patients { get; set; }

        public MatchPatientObject(int id)
        {
            KeyRepository Repo = new KeyRepository();
            Thisrecord = Repo.RecordbyID(id).Single();
            Patients = Repo.Patientmatch(Thisrecord);
            Hospital = Repo.HospitalbyID(Thisrecord.FacID).FirstOrDefault();
        }
    }
}